﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Domain.IRepository.MainData;
using FriendStar.Repository.MainData;

namespace FriendStar.Application.MainData
{
    /// <summary>
	/// 数据表 BUS_SJ_Dictionary 对应的数据应用功能类。
	/// </summary>
	public class BUS_SJ_DictionaryApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private IBUS_SJ_DictionaryRepository service = new BUS_SJ_DictionaryRepository();

        public List<BUS_SJ_DictionaryEntity> GetList(string itemId = "", string keyword = "")
        {
            var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(itemId))
            {
                expression = expression.And(t => t.ClassID == itemId);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FTypeNO.Contains(keyword) || t.FTypeName.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.SortCode).OrderBy(t => t.FTypeNO).ToList();
        }

        public List<BUS_SJ_DictionaryEntity> GetItemList(string FTypeNO)
        {
            var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FTypeNO == FTypeNO);
            return service.IQueryable(expression).OrderBy(t => t.SortCode).OrderBy(t => t.FTypeNO).ToList();
        }

        //public List<BUS_SJ_DictionaryEntity> GetItemList(string FTypeNO1, string FTypeNO2)
        //{
        //    var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
        //    expression = expression.And(t => t.UseUnit == UseUnit);
        //    expression = expression.And(t => t.FTypeNO == FTypeNO1 || t.FTypeNO == FTypeNO2);
        //    return service.IQueryable(expression).OrderBy(t => t.SortCode).OrderBy(t => t.FTypeNO).ToList();
        //}

        public List<BUS_SJ_DictionaryEntity> GetItemList(params string[] FTypeNOs)
        {
            var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            string extWhere = "";
            if (FTypeNOs != null && FTypeNOs.Length > 0)
            {
                extWhere += "(";
                for (int i = 0; i < FTypeNOs.Length; i++)
                {
                    if (i == 0)
                    {
                        extWhere += "FTypeNO='" + FTypeNOs[i] + "'";
                    }
                    else
                    {
                        extWhere += " OR FTypeNO='" + FTypeNOs[i] + "'";
                    }
                }
                extWhere += ")";
            }
            Pagination pagination = new Pagination()
            {
             page = 1, rows = int.MaxValue, sidx = "FTypeNO"
            };
            return service.FindList(expression, pagination, extWhere);
        }

        public BUS_SJ_DictionaryEntity GetForm(string keyValue)
        {
            var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
            expression = expression.And(t => t.FTypeID == keyValue);
            return service.FindEntity(expression);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.FTypeID == keyValue);
        }

        public void SubmitForm(BUS_SJ_DictionaryEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.FTypeID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedEntity(BUS_SJ_DictionaryEntity entity)
        {
            var expression = ExtLinq.True<BUS_SJ_DictionaryEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.ClassID == entity.ClassID);
            expression = expression.And(t => t.FTypeNO == entity.FTypeNO);
            if (!string.IsNullOrEmpty(entity.FTypeID))
            {
                expression = expression.And(t => t.FTypeID != entity.FTypeID);
            }
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }
    }
}